<?php

namespace Fronts\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController {

    private $CatalogTable;
    private $VideosTable;

    public function getCatalogTable() {
        if (!$this->CatalogTable) {
            $pst = $this->getServiceLocator();
            $this->CatalogTable = $pst->get("Fronts\Model\CatalogTable");
        }
        return $this->CatalogTable;
    }

    public function getVideosTable() {
        if (!$this->VideosTable) {
            $pst = $this->getServiceLocator();
            $this->VideosTable = $pst->get("Fronts\Model\VideosTable");
        }
        return $this->VideosTable;
    }

    public function indexAction() {
        $this->setlayout();
        $videoindex = $this->actionloadvideosindex();
        return array("list" => $videoindex);
    }

    private function setlayout() {
        $this->layout('layout/layout');
        $menuWidget = $this->forward()->dispatch('Fronts\Controller\Elements', array(
            'action' => 'menu',
        ));
        $videosnewwidget = $this->forward()->dispatch("Fronts\Controller\Elements", array(
            'action' => 'widgetvideosnews',
        ));
        $linkWidget= $this->forward()->dispatch("Fronts\Controller\Elements", array(
            'action' => 'widgetlink',
        ));
        $this->layout()->setVariable('listcatalog', $menuWidget);
        $this->layout()->setVariable('videosnewwidget', $videosnewwidget);
        $this->layout()->setVariable('linkWidget', $linkWidget);
    }

    private function actionloadvideosindex() {
        $listcatalog = $this->getCatalogTable()->loadcatalogindex();
        $listvideos = array();
        foreach ($listcatalog as $key => $listca) {
            $ctalogId = $listca["catalogId"];
            $listvideos[$key] = $this->getVideosTable()->loadvideosindex($ctalogId);
        }
        return array("listcatalog" => $listcatalog, "listvideos" => $listvideos);
    }

}
